#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@FileName  :task.py 
@desc      :celery的任务模块
@Author    :Young
@IDE       :PyCharm
@Time      :2021/3/21 0021 11:17
'''

#导入依赖
from celery.signals import task_success
from celery import shared_task
import time

from django.core.mail import send_mail

from Demo5.settings import EMAIL_FROM


@shared_task
def hello_world(n):
    for i in range(n):
        print("hello world")
        time.sleep(3)
#异步发送邮件
@shared_task
def send_a_mail(mail):
    """

    :param mail: 字典 {subject, message, from_email, recipient_list,
              fail_silently=False, auth_user=None, auth_password=None,
              connection=None, html_message=None}
    :return:
    """
    send_mail(**mail,from_email=EMAIL_FROM)  #邮件发送

#获取异步任务的执行结果
@shared_task
def sum_ent(n):
    result = 0
    for i in range(0,n+1,2):
        result += i
    return result

#获取异步任务的结果(回调函数)
@task_success.connect(sender=sum_ent)
def comlete(sender=None, result=None,**kwargs):
    print(result)


@shared_task
def do():
    print("今日事今日毕")
